package com.antu.first.service.impl;

import com.antu.first.bean.DiscountCoupon;
import com.antu.first.bean.Indent;
import com.antu.first.bean.User;
import com.antu.first.dao.DiscountCouponDao;
import com.antu.first.dao.IndentDao;
import com.antu.first.dto.IndentOperationExecution;
import com.antu.first.enums.IndentStateEnum;
import com.antu.first.exceptions.IndentOperationException;
import com.antu.first.service.IndentService;
import com.antu.first.util.CheckCodeUtil;
import com.antu.first.util.IndentSequenceCreator;
import com.antu.first.util.PageCaculator;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * Created by Administrator on 2018/3/5.
 */
@Service("indentService")
public class IndentServiceImpl implements IndentService {

    @Autowired
    private IndentDao indentDao;
    @Autowired
    private DiscountCouponDao discountCouponDao;

    @Override
    @Transactional
    public IndentOperationExecution insertIndent(Indent indent) {
        indent.setiStatus(0);
        indent.setiCreateTime(new Date());
        indent.setiSequence(IndentSequenceCreator.createSequence(indent.getUser().getuId()));
        try{
            indent.setiCheckCode(CheckCodeUtil.getRandomString(12));
            int effectNum=indentDao.insertIndent(indent);
            if(effectNum!=1){
                return new IndentOperationExecution(IndentStateEnum.ERROR);
            }
            //根据优惠券Id把优惠券状态设置为已使用，把优惠券状态设置为已使用
            if(indent.getDiscountCoupon() != null && indent.getDiscountCoupon().getdId()!= 0 && !"".equals(indent.getDiscountCoupon().getdId())){
                int effect = discountCouponDao.updateCouponsByIdAndStatus(indent.getDiscountCoupon().getdId(), 5);
                if(effect != 1){
                    throw new IndentOperationException("IndentServiceImpl-insertIndent-change DiscountCoupon Status：网路错误！");
                }
            }
        }catch(Exception e){
            return new IndentOperationExecution(IndentStateEnum.ERROR);
        }
        return new IndentOperationExecution(IndentStateEnum.SUCCESS,indent);
    }

    @Override
    public IndentOperationExecution queryIndentList(Date start, Date end, Indent indent, int pageIndex) {
        try{
            //统计该条件下的所有订单总量
            int count = indentDao.countIndent(start, end, indent);
            //如果传入的页数大于或者等于数据库中记录的总分页数，则返回一个提示信息
            if(pageIndex > (count/10)+1){
                return new IndentOperationExecution(IndentStateEnum.IS_LAST_PAGE);
            }else{
                //否则直接进行查询，并根据查询结果返回相应内容
                int rowIndex = PageCaculator.caculateRowIndex(pageIndex, 10);
                List<Indent> indents=indentDao.queryIndentList(start, end, indent, rowIndex);
                if(indents.size()>0){
                    IndentOperationExecution execution = new IndentOperationExecution(IndentStateEnum.SUCCESS, indents);
                    execution.setCount(count);
                    return execution;
                }else if(indents.size() == 0) {
                    return new IndentOperationExecution(IndentStateEnum.EMPTY_LIST);
                }else{
                    return new IndentOperationExecution(IndentStateEnum.ERROR);
                }
            }
        }catch(Exception e){
            throw new IndentOperationException("获取订单信息失败，错误信息："+e.getMessage());
        }
    }

    @Override
    @Transactional
    public IndentOperationExecution checkInHotel(int iId, String iSequence, String checkCode) {
        try{
            Indent indent = indentDao.queryIndentByCheckCodeAndSequence(iSequence, checkCode);
            if(indent == null){
                return new IndentOperationExecution(IndentStateEnum.DIFF_CODE);
            }
            int effectNum=indentDao.updateIndent(iId,iSequence, 1);
            if(effectNum!=1){
                return new IndentOperationExecution(IndentStateEnum.ERROR);
            }
        }catch(Exception e){
            throw new InputMismatchException("更新订单状态错误，错误信息："+e.getMessage());
        }
        return new IndentOperationExecution(IndentStateEnum.SUCCESS);
    }

    @Override
    @Transactional
    public IndentOperationExecution checkOutHotel(int iId, String iSequence) {
        try {
            int effectNum = indentDao.updateIndent(iId, iSequence, 22);
            if (effectNum!=1) {
                return new IndentOperationExecution(IndentStateEnum.ERROR);
            }
        } catch (Exception e) {
            throw new IndentOperationException("更新订单状态失败，错误信息："+e.getMessage());
        }
        return new IndentOperationExecution(IndentStateEnum.SUCCESS);
    }

    /**
     * 移动端根据用户编号查询订单
     * @param user
     * @return
     */
    public IndentOperationExecution getIndentListByIUserd(User user){
        Map<String,Object> map=new HashMap<String,Object>();
        List<Indent> list=new ArrayList();
        list=indentDao.getIndentListByIUserd(user);
        if(list.size()>0){
            return new IndentOperationExecution(IndentStateEnum.SUCCESS, list);
        }else{
            return new IndentOperationExecution(IndentStateEnum.ERROR);
        }
    }

    @Override
    public IndentOperationExecution queryIndents(int uId, int iStatus, int pageIndex) {
        try{
            int rowIndex = PageCaculator.caculateRowIndex(pageIndex, 10);
            List<Indent> indents=indentDao.queryIndents(uId, iStatus, rowIndex);
            if(indents.size()>0){
                IndentOperationExecution execution = new IndentOperationExecution(IndentStateEnum.LOADING, indents);
                execution.setCount(execution.getIndents().size());
                return execution;
            }else if(indents.size() == 0) {
                return new IndentOperationExecution(IndentStateEnum.EMPTY_LIST);
            }else{
                return new IndentOperationExecution(IndentStateEnum.ERROR);
            }
        }catch(Exception e){
            throw new IndentOperationException("获取订单信息失败，错误信息："+e.getMessage());
        }
    }

}
